﻿using PureMVC.Interfaces;
using PureMVC.Patterns;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

///<summary>
///
///</summary>
public class GameMediator : Mediator
{
    public new static string NAME = "GameMediator";
    GameView view;
    public GameView GameView
    {
        get {
            if (view == null)
            {
                view = GameObject.Find("Canvas/GameView").GetComponent<GameView>();
            }       
            return view;
        }
    }
    public override void OnRemove()
    {
        GameView.Open = null;
        GameView.Close = null;
        GameView.Exit = null;
        GameView.Rest = null;
        base.OnRemove();
    }
    public GameMediator():base(NAME)
    {
        GameView.Init();
        GameView.Open += () => { GameView.OpenPanel();};
        GameView.Close += () => { GameView.ClosePanel(); Time.timeScale = 1f;};
        GameView.Exit += () => {
            Time.timeScale = 1f;
            BalloonFactory.Instance.BalloonDontDestroy();
            GameView.AllEnemyDontDestroy();
            SendNotification(GameEvents.RETURN_START);
            StateManager.GetInstance.IsSkipStartScnen = true;
            GameConfig.EnemyCount = 0;
        };
        GameView.Rest += () => { SendNotification(GameEvents.REST_GAME); };
    }
    public override IList<string> ListNotificationInterests()
    {
        IList<string> notifications = new List<string>();
        notifications.Add(GameEvents.ADD);
        notifications.Add(GameEvents.GAME_OVER);
        notifications.Add(GameEvents.SET_PANEL);
        notifications.Add(GameEvents.RETURN_START);
        notifications.Add(GameEvents.REST_GAME);
		notifications.Add(GameEvents.RETARDER);
		notifications.Add(GameEvents.SPEED_UP);
		notifications.Add(GameEvents.CLEAR_ONE);
		notifications.Add(GameEvents.CLEAR_RANDOM_BALLOON);
        notifications.Add(GameEvents.LIGHTNING);
        notifications.Add(GameEvents.ADD_MONEY);
        return notifications;
    }
    public override void HandleNotification(INotification notification)
    {
        switch (notification.Name)
        {

            case GameEvents.ADD_MONEY:
                GameView.AddMoney((int)notification.Body);
                break;

            case GameEvents.REST_GAME:
                Time.timeScale = 1f;
                PlayerDataManager.GetIntance().Clear();
                GameView.RestGame();
                GameConfig.IsGameOver = false;
                GameView.SetEnemyRecycle();
                break;

            case GameEvents.GAME_OVER:
                StateManager.GetInstance.IsSkipEndPanel = true;
                break;

            case GameEvents.ADD:
                GameView.AddMoney((int)notification.Body);
                GameView.AddPoint();
                break;

			case GameEvents.RETARDER:
				Time.timeScale = 0.2f;
				GameView.Retarder();
				break;
			case GameEvents.SPEED_UP:
				GameView.Speed();
				break;
			case GameEvents.LIGHTNING:
				GameView.AwakeLightning();
				break;
			case GameEvents.CLEAR_ONE:
				GameView.ClearOneEnemy();
				break;
			case GameEvents.CLEAR_RANDOM_BALLOON:
				GameView.ClearOnBalloonType();
				break;
        }
    }
	

	
}
